2 Problem: 10249 - The grand dinner
3 Andrés Mejía-Posada (andmej@gmail.com)
28 #define foreach(x, v) for (typeof (v).begin() x = (v).begin(); x != (v).end(); ++x)
29 #define D(x) cout << #x " is " << x << endl
30 #define For(i, a, b) for (int i=(a); i<(b); ++i)
38 bool findPath(int n
, int src
, int sink
){
39 fill(prev
, prev
+n
, -1);
43 int u
= q
.front(); q
.pop();
44 if (u
== sink
) return true;
45 for (int v
= 0; v
<n
; ++v
){
46 if (v
!= src
&& prev
[v
] == -1 && flow
[u
][v
] < cap
[u
][v
]){
55 int maxFlow(int n
, int src
, int sink
){
56 memset(flow
, 0, sizeof flow
);
58 while (findPath(n
, src
, sink
)){
60 for (int u
= sink
; prev
[u
] != -1; u
= prev
[u
]){
61 neck
= min(neck
, cap
[prev
[u
]][u
] - flow
[prev
[u
]][u
]);
63 for (int u
= sink
; prev
[u
] != -1; u
= prev
[u
]){
64 flow
[prev
[u
]][u
] += neck
;
65 flow
[u
][prev
[u
]] -= neck
;
74 while (scanf("%d %d", &nteams
, &ntables
)==2 && nteams
&& ntables
){
76 const int source
= nteams
+ ntables
, sink
= source
+ 1;
77 memset(cap
, 0, sizeof cap
);
78 for (int i
=0, c
; i
<nteams
; ++i
){
83 for (int i
=0, c
; i
<ntables
; ++i
){
85 cap
[nteams
+i
][sink
] = c
;
87 for (int i
=0; i
<nteams
; ++i
){
88 for (int j
=0; j
<ntables
; ++j
){
92 int f
= maxFlow(nteams
+ ntables
+ 2, source
, sink
);
97 for (int i
=0; i
<nteams
; ++i
){
99 for (int j
=0; j
<ntables
; ++j
){
100 if (flow
[i
][nteams
+j
] > 0){
101 if (!first
) printf(" ");